home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Reference / DevCon / Washington_1988 / DevCon88.1 / Assembler / debug / numbers.asm next >
Encoding:
Assembly Source File  |  1992-08-27  |  4.1 KB  |  153 lines

  1.  
  2. ;
  3. ; Copyright (c) 1988 Commodore-Amiga, Inc.
  4. ;
  5. ; Executables based on this information may be used in software
  6. ; for Commodore Amiga computers.  All other rights reserved.
  7. ;
  8. ; This information is provided "as is"; no warranties are made.
  9. ; All use is at your own risk, and no liability or responsibility is assumed.
  10. ;
  11.  
  12. ;=============================================================================
  13. ; NAME
  14. ;    bin2asc[nn] - converts binary to a signed decimal string
  15. ;
  16. ; SYSNOPSIS
  17. ;    string=bin2asc[nn]( value,buffer )
  18. ;      D0           D0    A0
  19. ;
  20. ; FUNCTION
  21. ;    converts the given value to null terminated string of ascii digits
  22. ;
  23. ; INPUTS
  24. ;    value - the value to be converted
  25. ;    [nn]denotes the size of the operator being worked on, this can be
  26. ;    any of 8,16 or 32
  27. ;
  28. ;    buffer - where the string should go
  29. ;
  30. ; RESULT
  31. ;    string - pointer to the null terminated decimal string
  32. ;
  33. ; BUGS
  34. ;
  35. ; SEE ALSO
  36. ;
  37. ;============================================================================
  38.  
  39. bin2asc16    EXT.L    D0            make word into a longword
  40. bin2asc32    TST.L    D0            is it negative ?
  41.         BPL.S    bin2asc            nope, call main routine
  42.         NEG.L    D0            make positive
  43.         MOVE.B    #'-',(A0)+        output a minus sign
  44.  
  45. ;=============================================================================
  46. ; NAME
  47. ;    bin2asc - converts unsigned binary in D0 to ascii digits in a buffer
  48. ;
  49. ; SYSNOPSIS
  50. ;    number = bin2asc( value,buffer )
  51. ;      D0         D0    A0
  52. ;
  53. ; FUNCTION
  54. ;     converts the unsigned binary value in D0 to a null terminated string
  55. ;     of ascii digits beginning at the buffer position given
  56. ;
  57. ; INPUTS
  58. ;    value - 32 bit unsigned value
  59. ;
  60. ;    buffer - pointer to the buffer where the digits should be put
  61. ;
  62. ; RESULT
  63. ;    number is generated and original pointer is returned in D0
  64. ;
  65. ; BUGS
  66. ;
  67. ; SEE ALSO
  68. ;
  69. ;============================================================================
  70.  
  71. bin2asc        MOVEM.L    D2-D3/A2-A3,-(SP)
  72.         MOVEA.L    A0,A3            save buffer pointer
  73.         LEA.L    PowersOfTen(PC),A2    powers of ten table
  74.         MOVEQ.L    #0,D1            leading zero flag
  75.         MOVE.L    (A2)+,D2        fetch first power of ten
  76.  
  77. 10$        MOVEQ.L    #-1,D3            current digit value
  78. 20$        ADDQ.B    #1,D3            update this digit
  79.         SUB.L    D2,D0            subtract current pwr of 10
  80.         BPL.S    20$            go for another
  81.         ADD.L    D2,D0            fix, we went too far
  82.         TST.B    D3            is this a zero ?
  83.         BNE.S    30$            no, skip leading zero test
  84.         TST.W    D1            have we put in any digits yet?
  85.         BEQ.S    40$            no, so scrap this one
  86. 30$        MOVEQ.L    #1,D1            stop suppressing zeros now
  87.         ADDI.B    #'0',D3            make an ascii digit
  88.         MOVE.B    D3,(A0)+        store it
  89. 40$        MOVE.L    (A2)+,D2        get next power of 10
  90.         BNE.S    10$            do the next bit
  91.  
  92.         TST.W    D1            did we put any digits in
  93.         BNE.S    50$            yes
  94.         MOVE.B    #'0',(A0)+        no, put zero as the result
  95. 50$        CLR.B    (A0)            terminate the string
  96.         MOVE.L    A3,D0            retrieve pointer to buffer
  97.         MOVEM.L    (SP)+,D2-D3/A2-A3
  98.         RTS
  99.  
  100. PowersOfTen    DC.L    1000000000,100000000,10000000,1000000
  101.         DC.L    100000,10000,1000,100,10,1,0
  102.  
  103. ;=============================================================================
  104. ; NAME
  105. ;    bin2hex[nn] - converts a binary longword to a string of hex digits
  106. ;
  107. ; SYSNOPSIS
  108. ;    string = bin2hex[nn]( value,buffer )
  109. ;      D0             D0    A0
  110. ;
  111. ; FUNCTION
  112. ;    converts the value to a null terminated string of hex digits
  113. ;
  114. ; INPUTS
  115. ;    value - the value to be converted
  116. ;    [nn]denotes the size of the operator being worked on, this can be
  117. ;    any of 16 or 32
  118. ;
  119. ;    buffer - where the result will go
  120. ;
  121. ; RESULT
  122. ;    string - pointer to the string of hex digits (null terminated)
  123. ;
  124. ; BUGS
  125. ;
  126. ; SEE ALSO
  127. ;
  128. ;============================================================================
  129.  
  130. bin2hex32    MOVEQ.L    #7,D1            generate a longword
  131.         BRA.S    dohex
  132. bin2hex16    MOVEQ.L    #3,D1            generate a word
  133.  
  134. dohex        MOVE.L    D2,-(SP)
  135.         MOVE.L    D0,D2            save the value
  136.         MOVEA.L    A0,A1            save buffer address for later
  137.         CLR.B    1(A0,D1.W)        terminate the string
  138. 10$        MOVE.B    D2,D0            get next nybble
  139.         ANDI.B    #$0F,D0
  140.         ADDI.B    #'0',D0            make into ascii
  141.         CMPI.B    #'9',D0
  142.         BLE.S    20$            no adjustment for letters
  143.         ADDI.B    #('A'-'9')-1,D0        add offset for letters
  144. 20$        MOVE.B    D0,0(A0,D1.W)        store this digit
  145.         LSR.L    #4,D2            move next nybble into place
  146.         DBRA    D1,10$
  147.  
  148.         MOVE.L    A1,D0            return address of buffer
  149.         MOVE.L    (SP)+,D2
  150.         RTS
  151.  
  152.  
  153.